home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / VirtualLight / VLight1.3win32.exe / VibSDK / Samples / sample2.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-07  |  2.7 KB  |  118 lines

  1. /*
  2.  * VirtuaLight's binary .VIB format API, sample 2
  3.  * Written by Stephane Marty, 09/10/2001
  4.  *
  5.  * This sample program writes a binary VIB file
  6.  * describing a procedural spring made of polygons.
  7.  */
  8.  
  9. #include "..\vlBinDef.h"
  10.  
  11. #define TWO_PI (6.283185307179586476925287)
  12.  
  13. #define NU 180
  14. #define NV 24
  15. #define R1 0.5
  16. #define R2 0.2
  17. #define PERIOD 2
  18. #define CYCLES 5
  19.  
  20. static viVECTOR
  21. evaluate(double u, double v)
  22. {
  23.     viVECTOR q;
  24.  
  25.     viSetDbl(q.x, (1.0 - R1 * cos(v)) * cos(u));
  26.     viSetDbl(q.y, (1.0 - R1 * cos(v)) * sin(u));
  27.     viSetDbl(q.z, R2 * (sin(v) + u * PERIOD / M_PI));
  28.     return(q);
  29. }
  30.  
  31. void main(void)
  32. {
  33.     unsigned long pri=0;
  34.     int i, j;
  35.     double u, v, du, dv;
  36.     viPOLYGON    poly;
  37.     viDISK        disk;
  38.     viCAMERA        *cam;
  39.     viGENERAL        *gen;
  40.     viPOINT_LIGHT    *pl;
  41.     viFILE            *vib;
  42.    
  43.     du = CYCLES * TWO_PI / (double)NU;
  44.     dv = TWO_PI / (double)NV;
  45.  
  46.     // Open a new VIB file
  47.     vib = viNewBinaryVIB("sample2.vib");
  48.  
  49.     // Create and declare the procedural object named "spring"
  50.     viDeclareNewObject("spring", vib);
  51.     for (i=0; i<NU; i++)
  52.     {
  53.         u = i * du;
  54.         for (j=0; j<NV; j++)
  55.         {
  56.             v = j * dv;
  57.             // each polygon has 4 vertices...
  58.             viSetByte(poly.vertices, 4);
  59.             poly.vertex[0] = evaluate(u, v);
  60.             poly.vertex[1] = evaluate(u+du, v);
  61.             poly.vertex[2] = evaluate(u+du, v+dv);
  62.             poly.vertex[3] = evaluate(u, v+dv);
  63.             viDumpPolygon(&poly, vib);
  64.             pri++;
  65.         }
  66.     }
  67.     viEndObjectDeclaration(vib);
  68.     
  69.     // Add the camera (low adaptive antialiasing enabled)
  70.     cam = viNewCamera();
  71.     viSetInt(cam->Format.X, 256);
  72.     viSetInt(cam->Format.Y, 256);
  73.     viSetDbl(cam->FrameAspectRatio, 1.0);
  74.     viSetVector(&cam->Location, -2, -4, 5);
  75.     viSetVector(&cam->LookAt, 0, 0, 2);
  76.     viSetVector(&cam->UpAxis, 0, 0, 1);
  77.     viSetDbl(cam->FieldOfView, 60);
  78.     viSetInt(cam->Antialiasing, 1);
  79.     viDumpCamera(cam, vib);
  80.  
  81.     // Set the background color
  82.     gen = viNewGeneral();
  83.     viSetColor(&gen->Background, 0.5, 0.5, 0.5);
  84.     viDumpGeneral(gen, vib);
  85.  
  86.     // Add a key pointlight
  87.     pl = viNewPointLight();
  88.     viSetColor(&pl->Intensity, 1, 1, 1);
  89.     viSetVector(&pl->Position,
  90.         cam->Location.x*5,
  91.         cam->Location.y*2,
  92.         25);
  93.     viDumpPointLight(pl, vib);
  94.  
  95.     // Invoke the object
  96.     viCallObject("spring", vib);
  97.     viObjectShaderName("spring_shader", vib);
  98.     viEndObjectCall(vib);
  99.  
  100.     // Add a simple disk for the floor (detached from the object)
  101.     viPrimitive(vib);
  102.     viSetVector(&disk.center, 0, 0, 0);
  103.     viSetVector(&disk.normal, 0, 0, 1);
  104.     viSetDbl(disk.radius, 250);
  105.     viDumpDisk(&disk, vib);
  106.     viPrimitiveShaderName("ground", vib);
  107.     viEndPrimitive(vib);
  108.  
  109.     // Close the VIB file
  110.     viCloseBinaryVIB(vib);
  111.  
  112.     // Deallocate memory used
  113.     free(cam);
  114.     free(gen);
  115.     free(pl);
  116.  
  117.     fprintf(stderr, "\n%lu polygons dumped.\n", pri);
  118. }